{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: jieba in c:\\users\\administrator\\anaconda3\\lib\\site-packages (0.42.1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import os\n",
    "from nltk.parse import stanford"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分词结果: 2022 年 的 北京 冬奥会 是 第 24 届 冬季 奥林匹克运动会\n"
     ]
    }
   ],
   "source": [
    "string='2022年的北京冬奥会是第24届冬季奥林匹克运动会'\n",
    "seg_list=jieba.cut(string,cut_all=False,HMM=True)\n",
    "seg_str=' '.join(seg_list)\n",
    "print('分词结果:',seg_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.environ.get('JAVA_HOME'):\n",
    "    JAVA_HOME='C:\\Program Files\\Java\\jdk-21'\n",
    "    os.environ['JAVA_HOME']=JAVA_HOME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:5: DeprecationWarning: The StanfordParser will be deprecated\n",
      "Please use \u001b[91mnltk.parse.corenlp.StanforCoreNLPParser\u001b[0m instead.\n",
      "  \"\"\"\n"
     ]
    }
   ],
   "source": [
    "parser_path='stanford-parser.jar'\n",
    "model_path='stanford-parser-4.2.0-models.jar'\n",
    "pcfg_path='edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz'\n",
    "parser=stanford.StanfordParser(path_to_jar=parser_path,\n",
    "path_to_models_jar=model_path,model_path=pcfg_path)\n",
    "sentence=parser.raw_parse(seg_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "句法分析的结果:\n",
      " (ROOT\n",
      "  (IP\n",
      "    (NP\n",
      "      (DNP (QP (CD 2022) (CLP (M 年))) (DEG 的))\n",
      "      (NP (NR 北京))\n",
      "      (NP (NR 冬奥会)))\n",
      "    (VP\n",
      "      (VC 是)\n",
      "      (NP\n",
      "        (QP (OD 第))\n",
      "        (QP (CD 24) (CLP (M 届)))\n",
      "        (NP (NN 冬季) (NN 奥林匹克运动会))))))\n",
      "句法的叶子节点:\n",
      " ['2022', '年', '的', '北京', '冬奥会', '是', '第', '24', '届', '冬季', '奥林匹克运动会']\n"
     ]
    }
   ],
   "source": [
    "for line in sentence:\n",
    "    print('句法分析的结果:\\n',line)\n",
    "    print('句法的叶子节点:\\n',line.leaves())\n",
    "    line.draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
